package com.bbs.controller;

import com.bbs.entity.User;
import com.bbs.utils.RedisUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * redis 缓存测试控制器
 */
@Controller
public class RedisController {
    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping("/setRedisValue")
    @ResponseBody
    public String test(){
        redisUtil.set("name","klwu");
        return "success";
    }

    @RequestMapping("/getRedisValue")
    @ResponseBody
    public String test2(){
        Object name = redisUtil.get("name");
        System.out.println("my name is "+name);
        return "success";
    }

    @RequestMapping("/setList")
    @ResponseBody
    public String testList() throws JsonProcessingException {
        List<User> users = new ArrayList<>();
        User user = new User();
        user.setHeadUrl("wwww");
        users.add(user);
        // 引入jackjson包把list转json
        ObjectMapper objectMapper = new ObjectMapper();
        String userStr = objectMapper.writeValueAsString(users);
        System.out.println(userStr);
        boolean flag = redisUtil.set("userList",userStr);
        System.out.println("flag=="+flag);
        return "success";
    }

    @RequestMapping("/getList")
    @ResponseBody
    public String getList(){
        Object userList = redisUtil.get("userList");
        return userList.toString();
    }
}
